package com.lovo.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.boot.dao.UserDAO;
import com.lovo.boot.enums.ResultCode;
import com.lovo.boot.exception.GlobalHandleException;
import com.lovo.boot.model.dos.UserDO;
import com.lovo.boot.model.query.UserQuery;
import com.lovo.boot.model.vo.PageVO;
import com.lovo.boot.service.UserService;
import com.lovo.boot.util.PackPageUtil;
import com.lovo.boot.util.RedisUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @title
 * @author: luo
 * @date: 2020/11/24 14:03
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserDAO, UserDO> implements UserService {

    @Resource
    private UserDAO userDAO;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public UserDO findByName(String name) {
        return userDAO.selectByName(name);
    }

    @Override
    public PageVO<UserDO> findAll(UserQuery userQuery)  {

        Page<UserDO> page = new Page<>(userQuery.getPageNum() == null?1:userQuery.getPageNum(),
                userQuery.getPageSize() == null?5:userQuery.getPageSize());
        LambdaQueryWrapper<UserDO> wrapper = new QueryWrapper<UserDO>().lambda()
                .like(UserDO::getUserName, userQuery.getUserName() == null ? "" : userQuery.getUserName());
        Page<UserDO> userDOPage = userDAO.selectPage(page, wrapper);
        return PackPageUtil.packPage(userDOPage);
    }

    @Override
    public UserDO addUser(UserDO userDO) throws GlobalHandleException {

        UserDO user = userDAO.selectByName(userDO.getUserName());
        if (user != null) {
            throw new GlobalHandleException(ResultCode.USER_HAS_EXISTED);
        }
        userDO.setUserId(redisUtil.generateOrderId());
        int i = userDAO.insert(userDO);
        if (i<=0) {
            throw new GlobalHandleException(ResultCode.USER_REGISTER_ERROR);
        }
        return userDO;
    }

    @Override
    public void deleteUser(String userId) throws GlobalHandleException {
        int i = userDAO.deleteById(userId);
        if (i<=0){
            throw new GlobalHandleException(ResultCode.USER_NOT_EXIST);
        }
    }

    @Override
    public UserDO updateUser(UserDO userDO) throws GlobalHandleException {
        int i = userDAO.updateById(userDO);
        if (i<=0) {
            throw new GlobalHandleException(ResultCode.USER_UPDATE_ERROR);
        }
        return userDO;
    }

    @Override
    public UserDO login(UserDO userDO) throws GlobalHandleException {
//        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
//        wrapper.eq("user_name",userDO.getUserName());
//        wrapper.eq("user_pwd",userDO.getUserPwd());

        LambdaQueryWrapper<UserDO> lambdaQueryWrapper = new QueryWrapper<UserDO>().lambda()
                .eq(UserDO::getUserName, userDO.getUserName() == null ? "" : userDO.getUserName())
                .eq(UserDO::getUserPwd, userDO.getUserPwd() == null?"":userDO.getUserPwd());
        UserDO loginUser = userDAO.selectOne(lambdaQueryWrapper);
        if (loginUser == null) {
            throw new GlobalHandleException(ResultCode.USER_LOGIN_ERROR);
        }
        return loginUser;
    }

    @Override
    public UserDO findByUserId(String userId) throws GlobalHandleException {
        UserDO userDO = userDAO.selectById(userId);
        if (userDO == null) {
            throw new GlobalHandleException(ResultCode.USER_NOT_EXIST);
        }
        return userDO;
    }
}
